package cn.zmit.kuxi.activity;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.entity.HelpEntity;
import cn.zmit.kuxi.holder.HelpCenterHorlder;
import cn.zmit.kuxi.interfaces.OnListItemSelectListener;
import cn.zmit.kuxi.request.RequestTask;

/** * @author @parameter * @return */
public class HelpCenterActivity extends BaseActivity {
	@ViewInject(R.id.listView_help)
	private ListView listView_help; 
	private ListViewDataAdapter<HelpEntity>helpAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView("帮助中心", true);
		setContentView(R.layout.activity_help_center);
		ViewUtils.inject(this);
		initView();
		RequestTask.getInstance().getHelpCenter(this,new onRequestListener());//请求
	}
	private void initView(){//初始化view
		helpAdapter=new ListViewDataAdapter<HelpEntity>();
		helpAdapter.setViewHolderClass(this, HelpCenterHorlder.class);
		listView_help.setAdapter(helpAdapter);
		HelpCenterHorlder.setOnListItemSelectListener(new OnListItemSelectListener<HelpEntity>() {
			
			@Override
			public void onlistItemSelect(int position, HelpEntity itemDate) {
				Intent intent=new Intent();
				Bundle bundle =new Bundle();
				bundle.putString("id", itemDate.getId());
				intent.putExtras(bundle);
				intent.setClass(HelpCenterActivity.this, HelpDetailsActivity.class);
				startActivity(intent);
				
			}
		});
	}
  private class onRequestListener extends OnRequestListenerAdapter<Object>{
	  @Override
	public void onDone(XDroidRequest<?> request, String response, DataType dataType) {
		// TODO Auto-generated method stub
		super.onDone(request, response, dataType);
		JsonData jsonData=JsonData.create(response);
		JsonData data=jsonData.optJson("help_list");
		if (data!=null&&data.length()>0) {
			for (int i = 0; i < data.length(); i++) {
				JsonData itemDate=data.optJson(i);
				String id=itemDate.optString("id");
				String title=itemDate.optString("title");
				HelpEntity helpEntity=new HelpEntity(id, title);
				helpAdapter.append(helpEntity);
			}
		}
	}
  }
}
